//
//  TJStatusCell.swift
//  WeiBo
//
//  Created by TanJian on 15/12/10.
//  Copyright © 2015年 Joshpell. All rights reserved.
//

import UIKit
import SnapKit


class TJStatusCell: UITableViewCell {

    //toolbar的顶部约束
    var toolsbarTopConstraint: Constraint?
    //懒加载原生微博、工具栏和转发视图
    private lazy var originalView:TJOriginalView = TJOriginalView()
    private lazy var toolsbarView:TJToolsbarView = TJToolsbarView()
    private lazy var retweetView:TJRetweetView = TJRetweetView()
    
    //cell的视图模型,模型赋值时更新约束
    var userDataViewModel:TJUserDataViewModel?{
        didSet{
            
            originalView.statusViewModel = userDataViewModel
            toolsbarView.statusViewModel = userDataViewModel
            
            toolsbarTopConstraint?.uninstall()
            
            //
            if userDataViewModel?.status?.retweeted_status != nil{
                //有转发微博
                retweetView.hidden = false
                retweetView.statusViewModel = userDataViewModel
                
                //更新约束
                toolsbarView.snp_makeConstraints(closure: { (make) -> Void in
                    self.toolsbarTopConstraint = make.top.equalTo(retweetView.snp_bottom).constraint
                })
            }else{
                //无转发微博
                retweetView.hidden = true
                //更新约束
                toolsbarView.snp_makeConstraints(closure: { (make) -> Void in
                    self.toolsbarTopConstraint = make.top.equalTo(originalView.snp_bottom).constraint
                })

                
            }
            
            
            
        }
    }
    
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI(){
        
        //将三个模块添加到cell中
        contentView.addSubview(originalView)
        contentView.addSubview(toolsbarView)
        contentView.addSubview(retweetView)
        
        //添加各自的约束
        originalView.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(contentView)
            make.top.equalTo(contentView)
            make.trailing.equalTo(contentView)
        }
        
        retweetView.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(originalView)
            make.top.equalTo(originalView.snp_bottom)
            make.trailing.equalTo(originalView)
        }
        
        toolsbarView.snp_makeConstraints { (make) -> Void in
            self.toolsbarTopConstraint = make.top.equalTo(retweetView.snp_bottom).constraint
            
            make.leading.equalTo(contentView)
            make.width.equalTo(contentView)
            make.height.equalTo(35)
        }
        
        //最重要的约束，限制cell的大小
        contentView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(self)
            make.leading.equalTo(self)
            make.trailing.equalTo(self)
            make.bottom.equalTo(toolsbarView)
        }
    }
}








